/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.information;

import com.kmcsoft.core.ui.GenericController;
import com.kmcsoft.goldshop.information.service.InformationService;
import com.kmcsoft.goldshop.model.master.Currency;
import com.kmcsoft.goldshop.model.master.PawnInformation;
import com.kmcsoft.goldshop.model.master.PawnRate;
import com.kmcsoft.util.Utilities;
import com.kmcsoft.zkex.PagingListModel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import org.zkoss.zk.ui.Component;

/**
 *
 * @author AI-LAM
 */
public class PawnInformationComposer extends GenericController<PawnInformation, InformationService> {

    private PawnInformation parameter;
    private Collection<Currency> currencies;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        currencies = service.getCurrencies();
    }

    //<editor-fold defaultstate="collapsed" desc="onCreate">
    public void onCreate() {
        onClick$new(null);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="PagingListModel">
    @Override
    public PagingListModel getListModel() {
        String strFilter = "createdBy.name.matches('(?i).*" + filter.getValue().trim() + ".*')";
        PagingListModel<PawnInformation> model = Utilities.getPagingListModel(objectClass, HashMap.class, "id,createdBy.name,createdDate", strFilter);
        model.setOrdering("createdDate descending");
        model.initialize();
        return model;
    }

    public void setSelected(HashMap selected) {
        super.setSelected(service.getPawnInformationById(selected.get("id")));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doAfterNew">
    @Override
    protected void doAfterNew() {
        super.doAfterNew();
        if (getListModel().getSize() != 0) {
            parameter = (PawnInformation) service.getPawnInformationById(((HashMap) getListModel().getElementAt(0)).get("id"));
        }
        selected.setUse(true);
        if (parameter != null) {
            List<PawnRate> list = parameter.getPawnRates();
            int size = list.size();
            for (int i = 0; i < size; i++) {
                PawnRate object = (PawnRate) list.get(i);
                selected.getPawnRates().get(i).setRate(object.getRate());
            }

            selected.setPawnAmountException(parameter.getPawnAmountException());
            selected.setPawnDayException(parameter.getPawnDayException());
            selected.setPawnExpiredDate(parameter.getPawnExpiredDate());
            selected.setPawnLiquidationDate(parameter.getPawnLiquidationDate());
            selected.setPawnMinimumRate(parameter.getPawnMinimumRate());
            selected.setPawnRateException(parameter.getPawnRateException());
            selected.setPawnRatePerMonth(parameter.getPawnRatePerMonth());
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doBeforeSave">
    @Override
    protected void doBeforeSave() {
        super.doBeforeSave();
        checkBeforeSave();
        if (parameter != null) {
            parameter.setUse(false);
            service.makePersistent(parameter);
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="checkBeforeSave">
    private void checkBeforeSave() {
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="get/set">
    public Collection<Currency> getCurrencies() {
        return currencies;
    }

    public Collection<PawnRate> getOddPawnRates() {
        Collection<PawnRate> oddPawnRates = new ArrayList<PawnRate>();
        if (selected != null) {
            List<PawnRate> list = selected.getPawnRates();
            int size = list.size();
            for (int i = 0; i < size / 2; i++) {
                PawnRate obj = (PawnRate) list.get(i);
                oddPawnRates.add(obj);
            }
        }
        return oddPawnRates;
    }

    public Collection<PawnRate> getEvenPawnRates() {
        Collection<PawnRate> evenPawnRates = new ArrayList<PawnRate>();
        if (selected != null) {
            List<PawnRate> list = selected.getPawnRates();
            int size = list.size();
            for (int i = size / 2; i < size; i++) {
                PawnRate obj = (PawnRate) list.get(i);
                evenPawnRates.add(obj);
            }
        }
        return evenPawnRates;
    }
    //</editor-fold>
}
